package com.baitiaojun.moments.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.moments.UserMomentsStatus;
import com.baitiaojun.api.domain.user.User;
import com.baitiaojun.api.domain.user.UserInfo;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.api.vo.PageInfo;
import com.baitiaojun.moments.api.UserMomentsApi;
import com.baitiaojun.moments.feign.UserFeign;
import com.baitiaojun.moments.service.MomentsService;
import com.baitiaojun.moments.service.MomentsStatusService;
import com.baitiaojun.moments.support.UserSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @类名 MessageControllerImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/11/27 14:44
 * @版本 1.0
 */
@RestController
public class UserMomentsApiImpl implements UserMomentsApi {

    @Resource
    private MomentsService momentsService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserSupport userSupport;

    @Autowired
    private MomentsStatusService momentsStatusService;

    @Autowired
    private UserFeign userFeign;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public PageInfo getSubscribedMoments(Integer pageNo, Integer pageSize) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        return momentsService.getSubscribedUserMoments(userId, pageNo, pageSize);
    }

    @Override
    public JsonRes getCurrentUserMoments(Integer pageNo, Integer pageSize, Long userInfoId) throws Exception {
        Long userId;
        if (ObjectUtils.isEmpty(userInfoId)) {
            userId = userSupport.getCurrentUserId(request);
        } else {
            JsonRes jsonRes = userFeign.getOtherUserInfo(userInfoId);
            if (jsonRes.get("status").equals(500)) {
                throw new ConditionException("无法获取用户信息");
            }
            User user = JSONObject.parseObject(JSONObject.toJSONString(jsonRes.get("data")), User.class);
            UserInfo userInfo = user.getUserInfo();
            userId = userInfo.getUserid();
        }
        List<Object> moments = momentsService.getCurrentUserMoments(userId, pageNo, pageSize);
        return JsonRes.builder().success().data(moments);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes cancelMoments(Long id) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        momentsService.cancelUserMoments(id, userId);
        return JsonRes.builder().success();
    }

    @Override
    public JsonRes addMomentsStatus(UserMomentsStatus momentsStatus) throws ConditionException {
        momentsStatusService.addMomentsStatus(momentsStatus);
        return JsonRes.builder().success();
    }

    @Override
    public Integer getMomentsCount(Long userId) {
        return momentsService.getMomentsCount(userId);
    }
}